package com.wolfking.jeesite.condition;


import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;

@Order(Ordered.LOWEST_PRECEDENCE - 20)
class OnEnvironmentCondition extends SpringBootCondition {

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context,
                                            AnnotatedTypeMetadata metadata) {
        String env = (String) metadata.getAnnotationAttributes(ConditionalOnEnvironment.class.getName()).get("value");
        String active = context.getEnvironment().getProperty("spring.profiles.active");
        boolean result = StringUtils.isNotBlank(env) && env.equalsIgnoreCase(active);
        return new ConditionOutcome(result, ConditionMessage
                .forCondition(ConditionalOnEnvironment.class, "expression")
                .resultedIn(result));
    }
}
